package com.dibang.app.mvp.base;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;

import com.dibang.app.mvp.abs.AbsControl;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.ParameterizedType;

/**
 * Created by wxl on 2019/6/21.
 *
 */

public abstract class BaseActivity<C,CI extends AbsControl> extends AppCompatActivity {

    protected C control;

    private CI controlImpl;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        onCreateBindViewBefore(savedInstanceState);
        View view = onCreateBindViewUpdate(onCreateBindViewLayoutId(savedInstanceState), savedInstanceState);
        setContentView(view);
        controlImpl = generateControl(view);
        control = (C) controlImpl;
        onCreateBindViewChanged(savedInstanceState);
    }


    /**
     * 设置view之前
     *
     * @param savedInstanceState
     */
    protected void onCreateBindViewBefore(@Nullable Bundle savedInstanceState) {
        // todo child do something
    }

    /**
     * 布局id
     *
     * @return
     */
    protected abstract @LayoutRes int onCreateBindViewLayoutId(@Nullable Bundle savedInstanceState);


    /**
     * 设置view 之后
     */
    protected abstract void onCreateBindViewChanged(@Nullable Bundle savedInstanceState);


    /**
     * 默认获取布局的方式
     *
     * @return
     */
    private View generateLayoutView(@LayoutRes int layoutId) {
        return LayoutInflater.from(this).inflate(layoutId, null, false);
    }


    /**
     * 复写修改布局，改变最终设置的布局
     * @param layoutId
     * @return
     */
    protected View onCreateBindViewUpdate(@LayoutRes int layoutId,@Nullable Bundle savedInstanceState){
        return generateLayoutView(layoutId);
    }


    /**
     * 默认方式生成C层
     *
     * @return
     */
    protected CI generateControl(View targetView) {
        try {
            ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass();
            Class<CI> clazz = (Class<CI>) type.getActualTypeArguments()[1];
            Constructor c1 = clazz.getDeclaredConstructor(View.class);
            return (CI) c1.newInstance(targetView);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }



    @ColorInt
    public final int getResColor(@ColorRes int id) {
        return ContextCompat.getColor(this,id);
    }


    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    }


    @Override
    public void startActivity(Intent intent, Bundle options) {
        super.startActivity(intent, options);
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        super.startActivityForResult(intent, requestCode);
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    }

    @SuppressLint("RestrictedApi")
    @Override
    public void startActivityForResult(Intent intent, int requestCode, @Nullable Bundle options) {
        super.startActivityForResult(intent, requestCode, options);
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (controlImpl != null) {
            controlImpl.onDestroy();
        }
    }
}
